package com.hnxxxy.workshop.controller;

import com.hnxxxy.workshop.request.ProductPageRequest;
import com.hnxxxy.workshop.request.ProductRequest;
import com.hnxxxy.workshop.service.ProductService;
import com.hnxxxy.workshop.utils.R;
import com.hnxxxy.workshop.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 产品 前端控制器
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@RestController
@RequestMapping("/product")
@Api(value = "ProductController", tags = {"产品模块"})
public class ProductController {
    @Resource
    private ProductService productService;

    @PostMapping("/add")
    @ApiOperation(value = "创建产品")
    public R add(@ApiParam("产品对象") @RequestBody ProductRequest productRequest){
        productService.add(productRequest);
        return R.buildSuccess();
    }

    @GetMapping("/remove/{id}")
    @ApiOperation(value = "删除产品")
    public R deleteById(@ApiParam(value = "仓库id",required = true)
                        @PathVariable("id") int id){
        productService.deleteById(id);
        return R.buildSuccess();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改产品")
    public R update( @ApiParam("仓库产品")
                     @RequestBody ProductRequest productRequest){
        productService.update(productRequest);
        return R.buildSuccess();
    }

    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询产品信息")
    public R findByIdProduct(@ApiParam(value = "产品id",required = true)
                           @PathVariable("id") int id ){
        ProductVO productVO = productService.findByIdProduct(id);
        return R.buildSuccess(productVO);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询全部产品")
    public R findByIdFactory(@ApiParam(value = "分页对象",required = true)
                             @RequestBody ProductPageRequest productPageRequest){
        Map<String,Object> pageResult = productService.page(productPageRequest);
        return R.buildSuccess(pageResult);
    }
}

